Test Failed
Push — master ( 8314f3...393bed )
by Alexey
05:06
created

inji.onLoad   B

Complexity

Conditions 1
Paths 49

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 49
nop 0
dl 0
loc 78
rs 8.9019
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
C 0 31 8
A 0 22 3
B 0 15 11

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * Ecommerce Classes
3
 */
4
inji.Ecommerce = {
5
  Cart: new function () {
6
    this.addItem = function (itemOfferPriceId, count, btn, callback) {
7
      inji.Server.request({
8
        url: 'ecommerce/cart/add',
9
        data: {
10
          itemOfferPriceId: itemOfferPriceId,
11
          count: count
12
        },
13
        success: function (data) {
14
          if (callback) {
15
            callback(data, btn);
16
          }
17
          inji.Server.request({
18
            url: 'ecommerce/cart/getCart',
19
            success: function (data) {
20
              $("#cart,.cartplace").html(data);
21
            }
22
          });
23
        }
24
      }, btn);
25
    };
26
    this.calcSum = function (form) {
27
      if (form === undefined) {
28
        form = $('.ecommerce .cart-order_page form');
29
      }
30
      else {
31
        form = $(form)
32
      }
33
      var formData = new FormData(form[0]);
34
      $('.ecommerce .cart-order_page').prepend($('<div style = "position:absolute;width:' + $('.ecommerce .cart-order_page').width() + 'px;height:' + $('.ecommerce .cart-order_page').height() + 'px;background-color: rgba(255, 255, 255, 0.4);z-index:1000000"></div>'));
35
      inji.Server.request({
36
        url: form.attr('action'),
37
        type: 'POST',
38
        data: formData,
39
        dataType: 'html',
40
        processData: false,
41
        success: function (data) {
42
          $('.ecommerce .cart-order_page').html($(data).find('.ecommerce .cart-order_page').html());
43
          if ($(data).find('.alert').length > 0) {
44
            $.each($(data).find('.alert'), function () {
45
              //$('.ecommerce .cart-order_page').prepend(this.outerHTML)
46
            })
47
          }
48
        }
49
      });
50
    };
51
    this.delItem = function (cart_item_id, form) {
52
      $('.cart_item_id' + cart_item_id).remove();
53
      this.calcSum(form);
54
    };
55
    this.delItemWidget = function (cart_item_id, callback) {
56
      inji.Server.request({
57
        url: '/ecommerce/cart/deleteItem?cartItemId=' + cart_item_id,
58
        success: function (data) {
59
          $("#cart,.cartplace").html(data);
60
          if (callback !== undefined) {
61
            callback();
62
          }
63
        }
64
      });
65
    }
66
  },
67
  toggleFav: function (itemId, btn, noChangeText) {
68
    inji.Server.request({
69
      url: 'ecommerce/toggleFav/' + itemId,
70
      success: function (data) {
71
        $('.ecommerce-favorite-count').html(data.count);
72
        setTimeout(function () {
73
          if (!noChangeText) {
74
            $(btn).html(data.newText);
75
          }
76
        }, 100)
77
      }
78
    }, btn);
79
  }
80
};
81
inji.onLoad(function () {
82
83
  //plugin bootstrap minus and plus
84
  //http://jsfiddle.net/laelitenetwork/puJ6G/
85
  $('body').on('click', '.btn-number', function (e) {
86
    e.preventDefault();
87
88
    var fieldName = $(this).data('field');
89
    var type = $(this).data('type');
90
    var input = $("input[name='" + fieldName + "']");
91
    var currentVal = parseFloat(input.val());
92
    if (!isNaN(currentVal)) {
93
      if (type == 'minus') {
94
95
        if (currentVal > input.attr('min')) {
96
          input.val(currentVal - 1).change();
97
        }
98
        if (parseFloat(input.val()) == input.attr('min')) {
99
          $(this).attr('disabled', true);
100
        }
101
102
      } else if (type == 'plus') {
103
104
        if (currentVal < input.attr('max')) {
105
          input.val(currentVal + 1).change();
106
        }
107
        if (parseFloat(input.val()) == input.attr('max')) {
108
          $(this).attr('disabled', true);
109
        }
110
111
      }
112
    } else {
113
      input.val(0);
114
    }
115
  });
116
  $('body').on('focusin', '.input-number', function () {
117
    $(this).data('oldValue', $(this).val());
118
  });
119
  $('body').on('change', '.input-number', function () {
120
121
    var minValue = parseFloat($(this).attr('min'));
122
    var maxValue = parseFloat($(this).attr('max'));
123
    var valueCurrent = parseFloat($(this).val());
124
125
    var name = $(this).attr('name');
126
    if (valueCurrent >= minValue) {
127
      $(".btn-number[data-type='minus'][data-field='" + name + "']").removeAttr('disabled')
128
    } else {
129
      alert('Нельзя заказать меньше ' + minValue);
130
      $(this).val($(this).data('oldValue'));
131
    }
132
    if (valueCurrent <= maxValue) {
133
      $(".btn-number[data-type='plus'][data-field='" + name + "']").removeAttr('disabled')
134
    } else {
135
      alert('Извините, но больше нету');
136
      $(this).val($(this).data('oldValue'));
137
    }
138
139
140
  });
141
142
  $('body').on('keydown', ".input-number", function (e) {
143
    // Allow: backspace, delete, tab, escape, enter and .
144
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
145
      // Allow: Ctrl+A
146
      (e.keyCode == 65 && e.ctrlKey === true) ||
147
      // Allow: home, end, left, right
148
      (e.keyCode >= 35 && e.keyCode <= 39)) {
149
      // let it happen, don't do anything
150
      return;
151
    }
152
    // Ensure that it is a number and stop the keypress
153
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
154
      e.preventDefault();
155
    }
156
  });
157
158
})
159